<template>
  <a-modal title="货位详情" title-align="start" @ok="submit" :mask-closable="false" v-model:visible="showvisible" @cancel="showvisible = false" :width="900">
    <div class="mt10">
      <FilterTable :filterItem="filterItem" :isReset="true" :is-show-tag="false" @search="searchByVal" />
    </div>
    <a-space>
      <div>已选中</div>
      <div>{{ ids.length }}条</div>
    </a-space>
    <a-table size="mini" class="table-two-line mt10" ref="table" :loading="loading" row-key="id" v-model:selected-keys="ids" :row-selection="{ type: 'checkbox', showCheckedAll: true, width: 40 }" :data="packList" :pagination="false" :scroll="{ x: '100%', y: 400 }">
      <template #columns>
        <a-table-column title="图片" :width="60">
          <template #cell="{ record }">
            <ImgPopover :src="getImgUrl(record)">
              <template #con>
                <a-image :preview="false" width="40" height="40" fit="contain" :src="getImgUrl(record)" />
              </template>
            </ImgPopover>
          </template>
        </a-table-column>
        <a-table-column title="包装名称" #cell="{ record }" :width="60">
          <TextLimit position="top" :rows="2">{{ record.skuName }}</TextLimit>
        </a-table-column>
        <a-table-column title="SKU" #cell="{ record }" :width="60">
          <TextLimit position="top" :rows="2">{{ record.sku }}</TextLimit>
        </a-table-column>
        <a-table-column title="长宽高(cm)" #cell="{ record }" :width="60"> {{ $extract(record, 'volume.length') }}×{{ $extract(record, 'volume.width') }}×{{ $extract(record, 'volume.height') }} </a-table-column>
        <a-table-column title="重量(g)" #cell="{ record }" :width="60">
          {{ $extract(record, 'weight.weight') }}
        </a-table-column>
      </template>
    </a-table>
  </a-modal>
</template>

<script lang="ts" setup>
  import { computed, ref, onMounted } from 'vue'
  import { Message, Modal } from '@arco-design/web-vue'
  import { pagePacking } from '@/api/product'
  import FilterTable from '@/components/TableFilter.vue'
  import _ from 'lodash-es'

  let props = defineProps(['modelValue', 'info'])
  let emits = defineEmits(['update:modelValue', 'save'])
  let ids = ref([])
  let packList = ref([])
  let loading = ref(false)
  let searchValue: any = ref({})
  const filterItem = ref([
    {
      type: 'input_keyword',
      key: 'skuNameList',
      keyItems: [
        { label: '包装名称', value: 'skuNameList' },
        { label: 'SKU', value: 'skuList' },
      ],
      keyWidth: '100',
      value: ref(),
      placeholder: '请输入',
      label: '包装名称',
      width: 160,
      isMultiple: true,
    },
  ])

  let showvisible: any = computed({
    set(val) {
      emits('update:modelValue', val)
    },
    get() {
      return props.modelValue
    },
  })

  const getPackList = async () => {
    let params = {
      ...searchValue.value,
      pageSize: 99999,
    }
    loading.value = true
    const res: any = await pagePacking(params)
    loading.value = false
    packList.value = res?.data?.records || []
  }

  const getImgUrl = (record: any) => {
    return record.productPicList[0]?.url || ''
  }

  const searchByVal = (info: any) => {
    searchValue.value = _.cloneDeep(info)
    console.log(info)

    if (info.skuNameList) {
      searchValue.value.skuNameList = info.skuNameList.split(',')
    } else {
      searchValue.value.skuNameList = []
    }
    if (info.skuList) {
      searchValue.value.skuList = info.skuList.split(',')
    } else {
      searchValue.value.skuList = []
    }
    getPackList()
  }

  const submit = () => {
    if (ids.value.length == 0) return Message.error('请选择包装')
    let list: any = []
    ids.value.forEach((item: any) => {
      let info: any = packList.value.find((i: any) => i.id == item)
      info.num = 1
      list.push(info)
    })
    emits('save', list)
    showvisible.value = false
  }

  onMounted(() => {
    getPackList()
  })
</script>

<style lang="less" scoped>
  :deep(.arco-upload-picture-card) {
    min-width: 40px;
    height: 40px;
  }
  :deep(.arco-upload-list-picture) {
    width: 40px;
    height: 40px;
  }

  :deep(.arco-upload-list-picture-operation) {
    line-height: 40px;
  }

  .step-item {
    height: 14px;
    line-height: 14px;
  }

  .tab-box {
    display: flex;
    li {
      height: 30px;
      width: 60px;
      text-align: center;
      line-height: 30px;
      border: 1px solid #d7d7d7;
      border-right: none;
      &:last-child {
        border-right: 1px solid #d7d7d7;
      }
      font-size: 12px;
    }
    .active {
      background: #165dff;
      color: white;
    }
  }
</style>
